package com.yidao.menu.domain;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.util.CollectionUtils;

import com.yidao.core.domain.BaseObject;

public class Menu extends BaseObject{
	
	private static final long serialVersionUID = 1L;

	private String defaultSubItemId;

	private String id;
	
	private List<MenuItem> menuItems = new ArrayList<MenuItem>();
	
	private String name;

	public String getDefaultSubItemId() {
		return defaultSubItemId;
	}

	public String getId() {
		return id;
	}

	public MenuItem getItemById(List<MenuItem> menuItems,String id){
		for(MenuItem item : menuItems){
			String itemId = item.getId();
			if(itemId.equals(id)){
				return item;
			}else if(!CollectionUtils.isEmpty(item.getSubMenuItems())){
				MenuItem subItem = getItemById(item.getSubMenuItems(),id);
				if(subItem != null){
					return subItem;
				}
			}
		}
		return null;
	}

	public MenuItem getItemById(String id){
		return getItemById(this.menuItems,id);
	}
	
	public List<MenuItem> getMenuItems() {
		return menuItems;
	}
	
	public String getName() {
		return name;
	}
	
	public void setDefaultSubItemId(String defaultSubItemId) {
		this.defaultSubItemId = defaultSubItemId;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	public void setMenuItems(List<MenuItem> menuItems) {
		this.menuItems = menuItems;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}
